package com.hctforgreen.greenservice.threeLevelList;


import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.BookListEntity;
import com.hctforgreen.greenservice.model.SerieListEntity;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeBookEntity;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeChildSeriesEntity;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeCounts;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeId;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeLabel;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodePid;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeSeriesEntity;
import com.hctforgreen.greenservice.threeLevelList.customAnnotations.TreeNodeSize;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class TreeHelper {
	/**
	 * 传入我们的普通bean，转化为我们排序后的Node
	 *
	 * @param datas
	 * @param defaultExpandLevel
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel) throws IllegalAccessException {
		List<Node> result = new ArrayList<Node>();
		// 将用户数据转化为List<Node>
		List<Node> nodes = convetData2Node(datas);
		// 拿到根节点
		List<Node> rootNodes = getRootNodes(nodes);
		// 排序以及设置Node间关系
		for (Node node : rootNodes) {
			addNode(result, node, defaultExpandLevel, 1);
		}
		return result;
	}





	/**
	 * 过滤出所有可见的Node
	 *
	 * @param nodes
	 * @return
	 */
	public static List<Node> filterVisibleNode(List<Node> nodes) {
		List<Node> result = new ArrayList<Node>();

		for (Node node : nodes) {
			// 如果为跟节点，或者上层目录为展开状态
			if (node.isRoot() || node.isParentExpand()) {
				setNodeIcon(node);
				result.add(node);
			}
		}
		return result;
	}

	/**
	 * 将我们的数据转化为树的节点
	 *
	 * @param datas
	 * @return
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	public static <T> List<Node> convetData2Node(List<T> datas)
			throws IllegalArgumentException, IllegalAccessException

	{
		List<Node> nodes = new ArrayList<Node>();
		Node node = null;

		for (T t : datas) {
			String id = "";
			String pId = "";
			String label = null;
			String bookSize = "";
			int seriesCounts = -1;
			BookListEntity.BookEntity bookEntity = null;
			SerieListEntity.ChildSerieEntity childSerieEntity = null;
			SerieListEntity.SerieEntity seriesEntity = null;

			Class<? extends Object> clazz = t.getClass();
			Field[] declaredFields = clazz.getDeclaredFields();
			for (Field f : declaredFields) {
				if (f.getAnnotation(TreeNodeId.class) != null) {
					//设置访问权限，强制性的可以访问
					f.setAccessible(true);
					id = f.get(t).toString();
				}
				if (f.getAnnotation(TreeNodePid.class) != null) {
					f.setAccessible(true);
					pId = f.get(t).toString();
				}
				if (f.getAnnotation(TreeNodeLabel.class) != null) {
					f.setAccessible(true);
					label = (String) f.get(t);
				}
				if (f.getAnnotation(TreeNodeSize.class) != null) {
					f.setAccessible(true);
					bookSize = (String) f.get(t);
				}
				if (f.getAnnotation(TreeNodeCounts.class) != null) {
					f.setAccessible(true);
					seriesCounts = f.getInt(t);
				}
				if (f.getAnnotation(TreeNodeSeriesEntity.class) != null) {
					f.setAccessible(true);
					seriesEntity = (SerieListEntity.SerieEntity) f.get(t);
				}
				if (f.getAnnotation(TreeNodeBookEntity.class) != null) {
					f.setAccessible(true);
					bookEntity = (BookListEntity.BookEntity) f.get(t);
				}
				if (f.getAnnotation(TreeNodeChildSeriesEntity.class) != null) {
					f.setAccessible(true);
					childSerieEntity = (SerieListEntity.ChildSerieEntity) f.get(t);
				}
				if (!id.equals("") && !pId.equals("") && label != null
						&& bookSize != null && seriesCounts != -1 && bookEntity != null
						&& seriesEntity != null && childSerieEntity != null) {
					break;
				}
			}
			node = new Node(id, pId, label, bookSize, seriesCounts);
			node.setBookEntity(bookEntity);
			node.setSeriesEntity(seriesEntity);
			node.setChildSerieEntity(childSerieEntity);
			nodes.add(node);
		}

		/**
		 * 设置Node间，父子关系;让每两个节点都比较一次，即可设置其中的关系
		 */
		for (int i = 0; i < nodes.size(); i++) {
			Node n = nodes.get(i);
			for (int j = i + 1; j < nodes.size(); j++) {
				Node m = nodes.get(j);
				if (m.getpId().equals(n.getId())) {
					n.getChildren().add(m);
					m.setParent(n);
				} else if (m.getId().equals(n.getpId())) {
					m.getChildren().add(n);
					n.setParent(m);
				}
			}
		}

		// 设置图片
		for (Node n : nodes) {
			setNodeIcon(n);
		}
		return nodes;
	}
	public static List<Node> getRootNodes(List<Node> nodes)
	{
		List<Node> root = new ArrayList<Node>();
		for (Node node : nodes)
		{
			if (node.isRoot())
				root.add(node);
		}
		return root;
	}

	/**
	 * 把一个节点上的所有的内容都挂上去
	 */
	public static void addNode(List<Node> nodes, Node node,
								int defaultExpandLeval, int currentLevel)
	{

		nodes.add(node);
		if (defaultExpandLeval >= currentLevel)
		{
			node.setExpand(false);
		}

		if (node.isLeaf())
			return;
		for (int i = 0; i < node.getChildren().size(); i++)
		{
			addNode(nodes, node.getChildren().get(i), defaultExpandLeval,
					currentLevel + 1);
		}
	}

	/**
	 * 设置节点的图标
	 * 逻辑：（1）如果当前节点有孩子节点并且处于展开状态，那么设置向下的图标
	 * （2）如果当前节点有孩子节点并且处于闭合状态，那么设置向上的图标
	 * （3）如果当前节点没有孩子节点，传参-1，到时候判断是-1，不设置图标
	 * @param node
	 */
	private static void setNodeIcon(Node node)
	{
		if (node.getChildren().size() > 0 && node.isExpand())
		{
			node.setIcon(R.drawable.ic_on);
		} else if (node.getChildren().size() > 0 && !node.isExpand())
		{
			node.setIcon(R.drawable.ic_off);
		} else
			node.setIcon(-1);

	}
}
